package com.kuaimacode.kframework.api.jwt;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import com.kuaimacode.kframework.mybatis.models.user.User;
import com.kuaimacode.kframework.mybatis.models.user.UserRole;

public final class JwtUserFactory {

    private JwtUserFactory() {
    }

    public static JwtUser create(User user,List<UserRole> authorities) {
        return new JwtUser(
                user.getId(),
                user.getUsername(),
                user.getEmail(),
                user.getPhone(),
                user.getOpenId(),
                user.getUnionId(),
                user.getPassword(),
                mapToGrantedAuthorities(authorities),
                user.getEnabled() == 1,
                user.getLastpasswordresetDate()
        );
    }

    private static List<GrantedAuthority> mapToGrantedAuthorities(List<UserRole> authorities) {
        return authorities.stream()
                .map(authority -> new SimpleGrantedAuthority(authority.getRoleName()))
                .collect(Collectors.toList());
    }
}
